package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.domain.db.BlackList;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.dubbo.api.db.BlackListApi;
import com.tanhua.dubbo.mapper.BlackListMapper;
import com.tanhua.dubbo.mapper.UserInfoMapper;
import com.tanhua.vo.PageResult;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class BlackListApiImpl implements BlackListApi {

    @Autowired
    private BlackListMapper blackListMapper;


    @Override
    public PageResult queryBlacklist(Integer page, Integer pagesize, Long userId) {
//        要分页查询
//        select count(*) from tb_black_list where user_id=?
//
// SELECT u.* FROM tb_black_list b, tb_user_info u WHERE b.black_user_id=u.id  AND     b.user_id=1  LIMIT 0,10
        Page<UserInfo> page1 = new Page<>(page,pagesize);
        IPage<UserInfo> iPage = blackListMapper.queryBlacklist(page1, userId);
//        iPage.getTotal(); //总记录数
//        iPage.getRecords();// 当前页数据
        return new PageResult(page,pagesize,(int)iPage.getTotal(),iPage.getRecords());/// Integer page,Integer pagesize,int counts,List list
    }

    @Override
    public void deleteBlacklist(Long blacklistId, Long userId) {
//        DELETE FROM tb_black_list WHERE black_user_id=22 AND user_id=1

        QueryWrapper<BlackList> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BlackList::getBlackUserId,blacklistId);
        queryWrapper.lambda().eq(BlackList::getUserId,userId);
        blackListMapper.delete(queryWrapper);

    }


}
